//
//  ViewController.swift
//  HZJTab
//
//  Created by HZJ on 2017/4/6.
//  Copyright © 2017年 HZJ. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.randomColor()
        addFirstBtn()
        addSecondBtn()
        addThirdBtn()
        addFourthBtn()
        addFifthBtn()
    }

    func addFirstBtn(){
        let btn = UIButton(frame: CGRect(x: 0,y: 0,width: 100,height: 100))
        btn.setTitle(Localized("key"), for: UIControlState())
        btn.center = CGPoint(x: 100,y: 150)
        btn.backgroundColor = UIColor.randomColor()
        btn.addTarget(self, action: #selector(ViewController.clickTheFirstBtn), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btn)
    }
    func addSecondBtn(){
        let btn = UIButton(frame: CGRect(x: 0,y: 0,width: 100,height: 100))
        btn.setTitle(Localized("push"), for: UIControlState())
        btn.center = CGPoint(x: 300,y: 150)
        btn.backgroundColor = UIColor.randomColor()
        btn.addTarget(self, action: #selector(ViewController.clickTheSecondBtn), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btn)
    }
    func addThirdBtn(){
        let btn = UIButton(frame: CGRect(x: 0,y: 0,width: 100,height: 100))
        btn.setTitle(Localized("消有字红点"), for: UIControlState())
        btn.center = CGPoint(x: 100,y: 300)
        btn.backgroundColor = UIColor.randomColor()
        btn.addTarget(self, action: #selector(ViewController.clickTheThirdBtn), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btn)
    }
    func addFourthBtn(){
        let btn = UIButton(frame: CGRect(x: 0,y: 0,width: 100,height: 100))
        btn.setTitle(Localized("改有字红点"), for: UIControlState())
        btn.center = CGPoint(x: 300,y: 300)
        btn.backgroundColor = UIColor.randomColor()
        btn.addTarget(self, action: #selector(ViewController.clickTheFourthBtn), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btn)
    }
    func addFifthBtn(){
        let btn = UIButton(frame: CGRect(x: 0,y: 0,width: 100,height: 100))
        btn.setTitle(Localized("无字红点"), for: UIControlState())
        btn.center = CGPoint(x: 100,y: 450)
        btn.backgroundColor = UIColor.randomColor()
        btn.addTarget(self, action: #selector(ViewController.clickTheFifthBtn), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btn)
    }
    @objc func clickTheFirstBtn(){
        let old = UserDefaults.standard.object(forKey: "appLanguage") as! String
        
        let lan = old != "en" ? "en" : "zh-Hans"
        UserDefaults.standard.set(lan, forKey: "appLanguage")
        if let tab = self.tabBarController {
            (tab as! HZJTabBarController).myTab.reloadTheAllTabBarBtn()
        }
    }
    @objc func clickTheSecondBtn(){
        self.navigationController?.pushViewController(ViewController(), animated: true)
    }
    @objc func clickTheThirdBtn(){
        if let tab = self.tabBarController {
            (tab as! HZJTabBarController).myTab.btnArray[0].badgeValue = nil
        }
    }
    @objc func clickTheFourthBtn(){
        if let tab = self.tabBarController {
            (tab as! HZJTabBarController).myTab.btnArray[0].badgeValue = "\(arc4random_uniform(100))"
        }
    }
    @objc func clickTheFifthBtn(){
        if let tab = self.tabBarController {
            let old = (tab as! HZJTabBarController).myTab.btnArray[2].badgeValue
            (tab as! HZJTabBarController).myTab.btnArray[2].badgeValue = old == "" ? nil : ""
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismiss(animated: true, completion: nil)
    }
}

